package 基础语法.泛型;

import java.util.ArrayList;

public class 泛型 {
    public static void main(String[] args) {
        TestClass testClass = new TestClass();// 不指定泛型，默认为Object
        testClass.printJ(new Student(1, 2));
        testClass.printJ("String");

        TestClass<Student> testClass1 = new TestClass();// 指定泛型
        TestClass<Student> testClass2 = new TestClass<>();// 写法
        TestClass<Student> testClass3 = new TestClass<Student>();// 写法

        testClass1.printJ(new Student(0, 0));

        TestClass<Student> testClass4 = new TestClass(new Student(0, 0));
        testClass4.getJ().hello();// 无需强制转换，直接就是Student类型

        // testClass1.printJ("aa");//报错 ，若指定了泛型，在编译阶段报错。后续实际上还是Object但是多了编译检查
        TestClassMul<Student, Teacher> testClassMul = new TestClassMul();
        testClassMul.printJY(new Student(0, 0), new Teacher(0, 0));
         testClassMul.printJY(new Student(0, 0), new MathTeacher(0, 0));//多态
          TestClassMul<Student, MathTeacher> testClassMul1 = new TestClassMul();
          testClassMul.printJY(new Student(0, 0), new Teacher(0, 0));//多态



        TestMethod testMethod = new TestMethod();
        testMethod.printMeth1(new Student(0, 0), new Teacher(0, 0));
        testMethod.printMeth1("kkkkkk", 12);// Object

    }

}

class TestClass<J> {// 不确定的类型。感觉是在编译阶段 把这个J替换成Object或<>中指定的字符串替换掉？
    ArrayList<J> arrayList;
    J j;

    TestClass() {
    }

    TestClass(J j){this.j=j;};

    void printJ(J j) {// static 不能用static修饰
        System.out.println(j);
        if (j instanceof Student) {
            ((Student) j).hello();
        }

    }//

    J getJ() {
        return j;
    }
    <U> void method(){//里面也可也定义泛型方法

    }
}

class TestClassMul<J, Y> {// 想加多少加多少
    void printJY(J j, Y y) {// static 不能用static修饰
        System.out.println(j + "_" + y);
        if (j instanceof Student) {
            ((Student) j).hello();
        }
        if (y instanceof Teacher) {
            ((Teacher) y).hello();
        }

    }
}

class Student {
    int code;
    int age;

    Student(int code, int age) {
        this.code = code;
        this.age = age;
    }

    void hello() {
        System.out.println("我是小学生" + code);
    }

    @Override
    public String toString() {
        return "[" + code + "," + age + "]";
    }
}

class TestMethod {
    // 随便写几个
    public <Y, M> void printMeth1(Y y, M m) {
        System.out.println(m + "_" + y);
        if (y instanceof Student) {
            ((Student) y).hello();
        }
        if (m instanceof Teacher) {
            ((Teacher) m).hello();
        }

    }

    public static <T> void printMeth1(ArrayList<T> arrayList) {//这里可以用static修饰

    }

}

class Teacher {

    int code;
    int age;

    Teacher(int code, int age) {
        this.code = code;
        this.age = age;
    }

    void hello() {
        System.out.println("我是老师" + code);
    }

    @Override
    public String toString() {
        return "[" + code + "," + age + "]";
    }
}
class MathTeacher extends Teacher{

    MathTeacher(int code, int age) {
        super(code, age);
        //TODO Auto-generated constructor stub
    }
    
}